package view;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import app.SrtFix;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class ReplaceDialog extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text t_find;
	private Text t_place;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public ReplaceDialog(Shell parent) {
		super(parent, SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL);
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		int width = 300;
		int height = 100;
		shell.setSize(width, height);
		shell.setText("\u66FF\u6362\u6587\u672C");
		Point winPoint = SrtFix.app.shell.getLocation();
		Point winSize = SrtFix.app.shell.getSize();
		shell.setLocation(winPoint.x + (winSize.x - width) / 2, winPoint.y + (winSize.y - height) / 2);
		shell.setLayout(new GridLayout(3, false));

		Label lb_find = new Label(shell, SWT.NONE);
		lb_find.setText("\u67E5\u627E\uFF1A");

		t_find = new Text(shell, SWT.BORDER);
		t_find.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		new Label(shell, SWT.NONE);

		Label lb_replace = new Label(shell, SWT.NONE);
		lb_replace.setText("\u66FF\u6362\uFF1A");

		t_place = new Text(shell, SWT.BORDER);
		t_place.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Button btn_place = new Button(shell, SWT.NONE);
		btn_place.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				SrtFix.app.tableControl.replaceText(t_find.getText(), t_place.getText());
				shell.dispose();
			}
		});
		btn_place.setText("\u66FF\u6362\u5168\u90E8");
		btn_place.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));

	}

}
